<?php

/**
 * AdForm class.
 * AdForm is the data structure for keeping
 * web Ad form data. It is used by the 'ad' action of 'AdController'.
 */
class AdForm extends Banner
{
    public $ad;
    public $link;
    public $desc;
    public $target = Constants::TARGET_BLANK;
    public $created_at;
    public $updated_at;

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// website_title and website_url are required
            array('name','unique'),
			array('name', 'match', 'pattern' => '/^[a-zA-Z][0-9_]*/', 'message' => Yii::t('backend', 'Must begin with alphabet and can only includes alphabet,_,and number')),
			array('name, tips, input_type, type', 'required'),
            array('sort, autoload', 'numerical', 'integerOnly'=>true, 'min'=>0),
			array('id,value','safe'),
			//array('ad', 'file', 'types' => array('png', 'jpg', 'jpeg', 'gif', 'webp')),
			// 验证字符串 string
			//array('website_title, website_email, website_language, website_icp, website_statics_script, website_timezone, website_url, seo_title, seo_keywords, seo_description', 'boolean'),
			//array('smtp_port', 'numerical', 'integerOnly'=>true, 'min'=>0, 'max'=>65535),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
            'name' => Yii::t('app', '名称'),
            'input_type' => Yii::t('app', 'Ad Type'),
            'tips' => Yii::t('app', '描述'),
            'ad' => Yii::t('app', '广告类容'),
            'link' => Yii::t('app', 'Jump Link'),
            'desc' => Yii::t('app', 'Ad Explain'),
            'autoload' => Yii::t('app', 'Status'),
            'sort' => Yii::t('app', 'Sort'),
            'target' => Yii::t('app', 'Target'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
		);
	}

    /**
     * @return bool|void
     */
    public function afterDelete()
    {
        if( $this->input_type != Constants::AD_TEXT ){
            $file = Yii::getAlias('/uploads') . $this->ad;
            if( file_exists($file) && is_file($file) ) unlink($file);
        }
        return parent::beforeDelete();
    }


}
